<?php

class module_searchpicinfo extends module_base
{
	public function __construct()
	{
		parent::__construct();
		
		$this->_data_searchpicinfo	= new data_searchpicinfo();
		$this->_data_picclickrecord	= new data_picclickrecord();
	}
	
	//检查点击是否有效
	public function check_click($pic_id, $distance_time)
	{
		$current_time	= time() - $distance_time;
		
		//查询规定时间内是否点击
		$condition	= "pic_id = '$pic_id' AND click_time > '$current_time' ORDER BY id DESC";
		$click_info	= $this->_data_picclickrecord->fetch_one_field_by_condition($condition, 'pic_id');

		if(empty($click_info))
		{
			$insert_data['pic_id']		= $pic_id;
			$insert_data['ip']			= $_SERVER["REMOTE_ADDR"];
			$insert_data['click_date']	= date('Y-m-d H:i:s', time());
			$insert_data['click_time']	= time();
			
			//记录点击时间
			if($this->_data_picclickrecord->insert_row($insert_data))
			{
				$last_click_date = date('Y-m-d H:i:s', time());
				$this->_data_searchpicinfo->update_pic_click($pic_id, $last_click_date);
				
				return true;
			}
		}
		
		return false;
	}
	
	//更改照片大小
	public function change_pic_size($pic_id, $update_data)
	{			
		//更新点击后的图片大小
		if($this->_data_searchpicinfo->update_by_id($pic_id, $update_data))
		{
			//return $this->_data_searchpicinfo->fetch_one_field_by_id($pic_id, 'last_click_date');
			return true;
		}

		return false;
	}
	
	//上传寻找人信息
	public function upload_search($post, $files, $upload_file_path)
	{
		$search_id = 0;

		if((($files["pict"]["type"] == "image/gif") || ($files["pict"]["type"] == "image/jpeg") || ($files["pict"]["type"] == "image/pjpeg")) && ($files["pict"]["size"] < 200000))
		{
			if ($files["pict"]["error"] > 0)
		    {
		    	return $search_id;
		    	 //echo "Return Code: " . $files["pict"]["error"] . "<br />";
		    }
			else
		    {
		    	$pic_name	= md5($files["pict"]["tmp_name"]);
		    	$path_parts	= pathinfo($files["pict"]["name"]);
				$pic_type	= $path_parts["extension"];
		    	/*
			    echo "Upload: " . $files["pict"]["name"] . "<br />";
			    echo "Type: " . $files["pict"]["type"] . "<br />";
			    echo "Size: " . ($files["pict"]["size"] / 1024) . " Kb<br />";
			    echo "Temp file: " . $files["pict"]["tmp_name"] . "<br />";				
				
				if (file_exists($upload_file_path . $files["pict"]["name"]))
				{
					echo $files["pict"]["name"] . " already exists. ";
				}
				else
				{
				*/				
					move_uploaded_file($files["pict"]["tmp_name"], $upload_file_path . $pic_name . '.'.$pic_type);
					
					//入库
					$insert_pic_data['pic_url'] = $pic_name . '.'.$pic_type;
				
					$data_searchpicinfo = new data_searchpicinfo();
					if($pic_id = $data_searchpicinfo->insert_row($insert_pic_data))
					{
						$insert_search_data['search_title']		= $post['search_title'];
						$insert_search_data['search_name']		= $post['search_name'];
						$insert_search_data['search_intro']		= $post['search_intro'];
						$insert_search_data['contact_tel']		= $post['contact_tel'];
						$insert_search_data['contact_email']	= $post['contact_email'];
						$insert_search_data['contact_name']		= $post['contact_name'];
						$insert_search_data['contact_other']	= $post['contact_other'];
						$insert_search_data['search_pic_id']	= $pic_id;
						$insert_search_data['upload_date']		= date('Y-m-d H:i:s', time());
	
						$data_searchinfo = new data_searchinfo();
						if($search_id = @$data_searchinfo->insert_row($insert_search_data))
						{
							return $search_id;
						}
					}
				//}
			}
		}
		
		return false;
	}
}
